package com.gxy.learn.design.model.behaviour.interpreter;

import lombok.extern.slf4j.Slf4j;

/**
 * @description: 解释器模式 测试
 * @author: gaoxueyong
 * @Date: 2022/11/9 11:29
 */
@Slf4j
public class Client {

    public static void main(String[] args) {
//        创建环境变量
        Context context = new Context();
//        创建多个变量对象
        Variable a = new Variable("a");
        Variable b = new Variable("b");
        Variable c = new Variable("c");
        Variable d = new Variable("d");
//        变量存储在解释器中

        context.assign(a,1);
        context.assign(b,2);
        context.assign(c,3);
        context.assign(d,4);

//        获取抽象语法树 a+b-c+d
        AbstractExpression expression = new Plus(new Minus(new Plus(a,b),c),d);
        int result = expression.interprete(context);
        System.out.println(expression);
        log.info("{} = {}",expression.toString(),result);
    }

}
